﻿Public Class SEX

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASSEX As New PROVIDERSCLASS

    Private Sub SEX_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridsex()

        DesignForm(Me)
        DesignGrid(MASSEXGrid)

        PROVIDER_MASSEX.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridsex()
        Dim objSelectDatamassex As New massex
        Dim MASSEX As DataTable
        MASSEX = connect.GetTable(objSelectDatamassex.selectMassex_)
        MASSEXGrid.PrimaryGrid.DataSource = MASSEX
    End Sub

    Private Sub MASSEXGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASSEXGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเพศ").Value() IsNot DBNull.Value Then
            SEXIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเพศ").Value()
        Else
            SEXIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เพศ").Value() IsNot DBNull.Value Then
            SEXTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เพศ").Value()
        Else
            SEXTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            sexStatusCheckbox.Checked = True
        Else
            sexStatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE massex SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `sex` = '" & SEXIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASSEX SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [SEX] = '" & SEXIDTxt.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        'addVstatus.Enabled = False
        addSex.Enabled = False
    End Sub

    Private Sub addSex_Click(sender As Object, e As EventArgs)
        
    End Sub

    Private Sub editSex_Click(sender As Object, e As EventArgs)
       
    End Sub

    Private Sub addSex_Click_1(sender As Object, e As EventArgs) Handles addSex.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASSEX.PADD_ = True Then
                Dim objInsertData As New massex
                'objInsertData.sexID_ = CInt(sexIDTextbox.Text)
                objInsertData.sexDesc_ = SEXTxt.Text
                objInsertData.sexStatus_ = sexStatusCheckbox.Checked
                objInsertData.insertMassexData()

                SelectDatagridsex()

                SEXIDTxt.Text = ""
                SEXTxt.Text = ""
                sexStatusCheckbox.Checked = False
                addSex.Enabled = True
            Else
                MsgBox(PROVIDER_MASSEX.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editSex_Click_1(sender As Object, e As EventArgs) Handles editSex.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASSEX.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลเพศดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New massex
                    objEditData.sexID_ = CInt(SEXIDTxt.Text)
                    objEditData.sexDesc_ = SEXTxt.Text
                    objEditData.sexStatus_ = sexStatusCheckbox.Checked
                    objEditData.editMassexData()

                    SelectDatagridsex()

                    SEXIDTxt.Text = ""
                    SEXTxt.Text = ""
                    sexStatusCheckbox.Checked = False
                    addSex.Enabled = True
                End If
                addSex.Enabled = True
            Else
                MsgBox(PROVIDER_MASSEX.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(massex.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(massex.Text)
        nextform.ShowDialog()
    End Sub
End Class